﻿using System;
using System.IO;

namespace StockMarketTool.Helper
{
    public class LogHelper
    {
        private static string logUrl = AppDomain.CurrentDomain.BaseDirectory + "Logs";

        public static void LogError(Exception ex, string title = "")
        {
            if (!Directory.Exists(logUrl))
            {
                Directory.CreateDirectory(logUrl);
            }

            string logFile = logUrl + $"/Error-{DateTime.Now.ToString("yyyy-MM-dd")}.txt";
            if (!File.Exists(logFile))
            {
                FileStream fs = File.Create(logFile);
                fs.Close();
            }
            WriteLog(logFile, title + ex.StackTrace);
        }

        public static void Log(string msg)
        {
            //if (Program.IsLogData == "0")
            //{
            //    return;
            //}

            if (!Directory.Exists(logUrl))
            {
                Directory.CreateDirectory(logUrl);
            }

            string logFile = logUrl + $"/Log-{DateTime.Now.ToString("yyyy-MM-dd")}.txt";
            if (!File.Exists(logFile))
            {
                FileStream fs = File.Create(logFile);
                fs.Close();
            }
            WriteLog(logFile, msg);
        }

        private static void WriteLog(string path, string logMsg)
        {
            StreamWriter sw = new StreamWriter(path, true, System.Text.Encoding.Default);
            sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "-->" + logMsg);
            sw.Close();
        }
    }
}
